package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.CommunicateInfo;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.AddCommunicationRecordView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/9/4 0004.
 * 说明：
 */

public class AddCommunicationRecordPersenter extends BasePresent<AddCommunicationRecordView> {

    RetrofitSerives retrofitSerives;

    Call<ResultMessage> addCommunicationCall;
    Call<CommunicateInfo> communicateInfoCall;

    public void getAddCommunicate(String jsonParams) {

        L.i("getAddCommunicate", "jsonParams--------->" + jsonParams);

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        addCommunicationCall = retrofitSerives.getAddCommunicate(body);

        addCommunicationCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {

                        view.setAddCommunicate(response.body());
                    } else {

                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {//Response{protocol=http/1.1, code=400, message=, url=http://47.93.78.124:9100/dekor_test/app/related/customer/addCommunicate}
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getCommunicateInfo(Integer customerId, Integer userManagerId) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customer_id", customerId);
            jsonObject.put("userManager_id", userManagerId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String jsonParams;

        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        communicateInfoCall = retrofitSerives.getCommunicateInfo(body);

        communicateInfoCall.enqueue(new Callback<CommunicateInfo>() {
            @Override
            public void onResponse(Call<CommunicateInfo> call, Response<CommunicateInfo> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0") || response.body().getCode().equals("2")) {

                        view.setCommunicateInfo(response.body());
                    } else {

                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CommunicateInfo> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
